package tazadum.engine.ui;

import java.lang.reflect.Field;

class Parameter {
	final String name;
	final String field;
	final String clazz;
	final Object obj;
	float value;

	public Parameter(final String name, final String field, final Object obj) {
		this.name = name;
		this.field = field;
		this.clazz = obj.getClass().getSimpleName();
		this.obj = obj;

		poll();
	}

	public float value() {
		return value;
	}

	private void poll() {
		try {
			final Field fieldRef = obj.getClass().getDeclaredField(field);
			fieldRef.setAccessible(true);
			value = fieldRef.getFloat(obj);
		} catch (final Exception e) {
			System.err.println("Problem polling from " + clazz + ":" + field);
		}
	}

	public void push(final float value) {
		try {
			this.value = value;
			final Field fieldRef = obj.getClass().getDeclaredField(field);
			fieldRef.setAccessible(true);
			fieldRef.setFloat(obj, value);
		} catch (final Exception e) {
			System.err.println("Problem pushing to " + clazz + ":" + field);
		}
	}
}
